//
//  CompanionsCommunity.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/10.
//  Copyright © 2016年 KX. All rights reserved.
#import "FreeTravelMacro.h"
#import "FreeTravelNetwork.h"
#import "WebViewOfCommendViewController.h"
#import "ModelOfCompanionsCommunity.h"
#import "ModelOfCompanionsCommunityData.h"
#import "CompanionsCommunity.h"
#import "CellOfCompanionsCommunity.h"
#import "CommunityViewController.h"
#import "TextAdaptation.h"
#import "UIImageView+WebCache.h"
@interface CompanionsCommunity ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ModelOfCompanionsCommunity *model;
@end
@implementation CompanionsCommunity
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self handleData];
        [self createTableView];
    }
    return self;
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"CellOfCompanionsCommunity" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CellOfCompanionsCommunity"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellOfCompanionsCommunity *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfCompanionsCommunity"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.model.data[indexPath.row];
    [cell.ImageViewOfAvatar sd_setImageWithURL:[NSURL URLWithString:cell.model.avatar]];
    cell.labelOfUsername.text = cell.model.username;
    cell.labelOfCity.text = [NSString stringWithFormat:@"|%@",cell.model.citys_str];
    cell.labelOfTitle.text = cell.model.title;
    cell.labelOfViews.text = [NSString stringWithFormat:@"%ld人浏览",cell.model.views];
    cell.labelOfReplys.text = [NSString stringWithFormat:@"%ld评论",cell.model.replys];
    cell.labelOfPublish.text = [self distanceTimeWithBeforeTime:[cell.model.publish_time doubleValue]];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[cell.model.start_time integerValue]];
    NSString *timeStr = [formatter stringFromDate:date];
    
//    NSDateFormatter * formatter1 = [[NSDateFormatter alloc] init];
//    [formatter setDateStyle:NSDateFormatterMediumStyle];
//    [formatter setTimeStyle:NSDateFormatterShortStyle];
//    [formatter setDateFormat:@"yyyy年MM月dd日HH:MM:ss"];
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:[cell.model.end_time integerValue]];
    NSString *timeStr1 = [formatter stringFromDate:date1];
    
    cell.labelOfTime.text = [NSString stringWithFormat:@"%@ - %@",timeStr,timeStr1];
    
    return cell;
}
- (NSString *)distanceTimeWithBeforeTime:(double)beTime
{
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    double distanceTime = now - beTime;
    NSString * distanceStr;
    
    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];
    
    [df setDateFormat:@"dd"];
    NSString * nowDay = [df stringFromDate:[NSDate date]];
    NSString * lastDay = [df stringFromDate:beDate];
    
    if (distanceTime < 60) {//小于一分钟
        distanceStr = @"刚刚";
    }
    else if (distanceTime <60*60) {//时间小于一个小时
        distanceStr = [NSString stringWithFormat:@"%ld分钟前",(long)distanceTime/60];
    }
    else if(distanceTime <24*60*60 && [nowDay integerValue] == [lastDay integerValue]){//时间小于一天
        distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
    }
    else if(distanceTime<24*60*60*2 && [nowDay integerValue] != [lastDay integerValue]){
        
        if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
            distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr];
        }
        else{
            [df setDateFormat:@"MM-dd HH:mm"];
            distanceStr = [df stringFromDate:beDate];
        }
        
    }
    else if(distanceTime <24*60*60*365){
        [df setDateFormat:@"MM-dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];
    }
    else{
        [df setDateFormat:@"yyyy-MM-dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];
    }
    return distanceStr;
}
- (void)handleData{
    
    [FreeTravelNetwork GET:@"http://open.qyer.com/qyer/company/search_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=7.0.1&track_device_info=iPhone%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%25209.3.4&v=1" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.model =[ModelOfCompanionsCommunity modelWithDic:result];
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WebViewOfCommendViewController *vc = [[WebViewOfCommendViewController alloc]init];
    ModelOfCompanionsCommunityData *model = self.model.data[indexPath.row];
    vc.url = model.appview_url;
    CommunityViewController *community = (CommunityViewController *)self.nextResponder.nextResponder.nextResponder;
    [community.navigationController pushViewController:vc animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ModelOfCompanionsCommunityData *model = self.model.data[indexPath.row];
    CGSize size = [TextAdaptation labelRectWithSize:CGSizeMake(SCREEN_WIDTH-80, CGFLOAT_MAX) LabelText:model.title Font:[UIFont systemFontOfSize:17]];
    
        return size.height+150;
   
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.tableView.frame = self.contentView.bounds;
}
@end
